package com.zpself.module.basic.序列化;

import com.google.common.collect.Lists;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;

import java.io.*;
import java.util.ArrayList;

/**
 * 序列化和反序列化的操作
 * */
public class serDemo{
    byte buf[];
    public static void main(String[] args) throws Exception{
        ser(); // 序列化
        dser(); // 反序列话
    }
  
    public static void ser()throws Exception{
        File file = new File("C:\\Users\\ying\\Desktop\\gc" + File.separator + "hello.txt");

        ArrayList<Person1> objects = Lists.newArrayList();
        Person1 person ;
        for (int i = 0; i < 30; i++) {
            person = new Person1( RandomStringUtils.randomAlphanumeric(3), RandomUtils.nextInt(0,100));
            objects.add(person);
        }
        System.out.println(objects);
        ObjectOutputStream out= new ObjectOutputStream(new FileOutputStream(file));
        out.writeObject(objects);
        out.close();
    }
  
    public static void dser()throws Exception{
        File file = new File("C:\\Users\\ying\\Desktop\\gc" + File.separator + "hello.txt");
        FileInputStream fi = new FileInputStream(file);
        ObjectInputStream input = new ObjectInputStream(new FileInputStream(file));

        byte[] b = new byte[10];
        fi.read(b);
        System.out.println("打印字节数组...每个字节");
        for (int i = 0; i < b.length; i++) {
            byte b1 = b[i];
            System.out.print(b1+",");
        }
        System.out.println();
        ArrayList<Person1> objects = (ArrayList<Person1>) input.readObject();
        input.close();
       System.out.println(objects);
    }
}